
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Poster2Main {
    public static void main(String[] args) throws Exception {


	GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String[] fa=ge.getAvailableFontFamilyNames();
        for (String s:fa) {
            System.out.println(s);
        }	

        String qrCodeUrl = "https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vca89c61b2c1c5"; //二维码
        String avatarUrl = "https://wework.qpic.cn/wwhead/duc2TvpEgSSGEUia6smCyfqicBic4aHXMcyTIficp0f7Ow11v9XRqyOGs/0"; //头像
        String name = "张红";
        String position = "置业顾问";
        String project = "找不到项目信息";
        String company = "地Gedle";
        String tel = "13764970002";
        String bottomInfo = "扫描二维码，添加我的企业微信";

        int width = 300;//300;
        int height = 514;//514;

	String fontFilePath = "./SourceHanSansSC-VF.ttf";
	File fontFile = new File(fontFilePath);
        Poster poster = initPoster(width,height,avatarUrl,qrCodeUrl,name,position,project,company,tel,bottomInfo,fontFile);
        PosterUtil.drawPoster(poster);
    }

    private static Poster initPoster(int width,int height,String avatarUrl,String qrCodeUrl,String name,String position,String project,String company,String tel,String bottomInfo,File fontFile) {
        Poster poster = new Poster();
	double baseN = 1.6d;//2.4d;
        //画布
        poster.setWidth((int)(baseN * width));
        poster.setHeight((int)(baseN * height));
        //头像
        poster.setAvatarUrl(avatarUrl);
        poster.setAvatarX(0);
        poster.setAvatarY(0);
        poster.setAvatarWidth((int)(baseN * 300));//300
        poster.setAvatarHeight((int)(baseN * 300));//300

        //小程序码
        poster.setQrCodeUrl(qrCodeUrl);
        poster.setQrCodeWidth((int)(baseN * 90));//90
        poster.setQrCodeHeight((int)(baseN * 90));//90
        poster.setQrCodeX((int)(baseN * 188));//188
        poster.setQrCodeY((int)(baseN * 360));//360

        //名字
        poster.setName(name);
	Font nameFont = null;
	if( null != fontFile){
	  nameFont = getSelfDefinedFont(fontFile);
	  nameFont = nameFont.deriveFont(Font.BOLD, (int)(baseN * 18));
	}else{
	  nameFont = new Font("SourceHanSansSC",Font.BOLD,(int)(baseN * 18));
	}
        poster.setNameFont(nameFont);
        poster.setNameColor(new Color(51,51,51));
        poster.setNameX((int)(baseN*18));//18
        poster.setNameY((int)(baseN*326));//326

        //岗位
        poster.setPosition(position);
	
	Font positionFont = null;
        if( null != fontFile){
          positionFont = getSelfDefinedFont(fontFile);
	  positionFont = positionFont.deriveFont(Font.PLAIN, (int)(baseN * 13));
        }else{
          positionFont = new Font("SourceHanSansSC",Font.PLAIN,(int)(baseN * 13));
        }

        poster.setPositionFont(positionFont);
        poster.setPositionColor(new Color(119,119,119));
        poster.setPositionX((int)(baseN*18));
        poster.setPositionY((int)(baseN*346));

        //项目信息
        poster.setProject(project);
        
	Font projectFont =  null;
        if( null != fontFile){
          projectFont = getSelfDefinedFont(fontFile);
	  projectFont = projectFont.deriveFont(Font.PLAIN, (int)(baseN * 15));
        }else{
          projectFont = new Font("SourceHanSansSC",Font.PLAIN,(int)(baseN * 15));
        }

	poster.setProjectFont(projectFont);
        poster.setProjectColor(new Color(51,51,51));
        poster.setProjectX((int)(baseN*18));
        poster.setProjectY((int)(baseN*407));


        //company
        poster.setCompany(company);
        
	Font companyFont = null;
        if( null != fontFile){
          companyFont =  getSelfDefinedFont(fontFile);
	  companyFont = companyFont.deriveFont(Font.BOLD, (int)(baseN * 18));
        }else{
          companyFont = new Font("SourceHanSansSC",Font.BOLD,(int)(baseN * 18));
        }

	
	poster.setCompanyFont(companyFont);
        poster.setCompanyColor(new Color(51,51,51));
        poster.setCompanyX((int)(baseN*18));
        poster.setCompanyY((int)(baseN*380));

        //tel
        poster.setTel(tel);
        
	Font telFont = null;
        if( null != fontFile){
          telFont = getSelfDefinedFont(fontFile);
	  telFont = telFont.deriveFont(Font.PLAIN, (int)(baseN * 15));
        }else{
          telFont = new Font("SourceHanSansSC",Font.PLAIN,(int)(baseN * 15));
        }

	
	poster.setTelFont(telFont);
        poster.setTelColor(new Color(51,51,51));
        poster.setTelX((int)(baseN*18));
        poster.setTelY((int)(baseN*431));

        //底部栏 bottom
        poster.setBottomInfo(bottomInfo);
        
	Font bottomFont = null;
        if( null != fontFile){
          bottomFont = getSelfDefinedFont(fontFile);
	  bottomFont = bottomFont.deriveFont(Font.PLAIN, (int)(baseN * 13));
        }else{
          bottomFont = new Font("SourceHanSansSC",Font.PLAIN,(int)(baseN * 13));
        }

	poster.setBottomFont(bottomFont);
        poster.setBottomColor(new Color(119,119,119));
        poster.setBottomX((int)(baseN*55));
        poster.setBottomY((int)(baseN*476));

        return poster;
    }

    private static java.awt.Font getSelfDefinedFont(File fontFile){
        java.awt.Font font = null;
        try{
            font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);
        }catch (FontFormatException e){
            return null;
        }catch (FileNotFoundException e){
            return null;
        }catch (IOException e){
            return null;
        }
        return font;
    }
    private static java.awt.Font getSelfDefinedFont(String  fontFilePath){
        java.awt.Font font = null;
	File fontFile = new File(fontFilePath);
        try{
            font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);
        }catch (FontFormatException e){
            return null;
        }catch (FileNotFoundException e){
            return null;
        }catch (IOException e){
            return null;
        }
        return font;
    }

}
